package semantic_compute.relation_extraction.yago_matcher;

import knowlwdge_graph.knowledge_etl.yago_etl.ExtractorTSVFiles;
import knowlwdge_graph.knowledge_etl.yago_etl.RowYagoTriple;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by julianzliu on 4/23/2017.
 */
public class TransformerRelation {


    static final org.slf4j.Logger logger = LoggerFactory.getLogger(TransformerRelation.class);

    private LoderYagoRelation loderYagoRelation;
    private ExtractorTSVFiles extractorTSVFiles;
    private ExtractorFromPostgres extractorFromPostgres;
    private ExtractorFromPatty extractorFromPatty;

    public TransformerRelation(){
        this.loderYagoRelation = new LoderYagoRelation();
        this.extractorTSVFiles = new ExtractorTSVFiles();
        this.extractorFromPostgres = new ExtractorFromPostgres();
        this.extractorFromPatty = new ExtractorFromPatty();
    }



    private List<RowYagoTriple> getRelationSchema(String file){
        return this.extractorTSVFiles.getAllTripleByTSVParser( file );
    }




    public void importRelationGlosses(){
        List<RowYagoTriple> relations = getRelationSchema("/home/gammaliu/Datas/yago/yago3/yagoRelations/yagoSchema.tsv");
        Set<String> relationSet = new HashSet<>();
        for(Integer i = 1; i < relations.size(); i++){
            RowYagoTriple triple = relations.get(i);
            relationSet.add(  triple.subject );
        }
        for(String subject: relationSet){
            //RowYagoTriple triple = relations.get(i);
            List<RowYagoTriple> glossList = this.extractorFromPostgres.getRelatonGloss(subject);
            for (RowYagoTriple gloss: glossList){
                this.loderYagoRelation.saveRelatonGlossTripleTuple(gloss );
            }
        }
    }


    public void importRelationPatty(){
        List<RowYagoTriple> pattys = this.extractorFromPatty.getAllTripleByTSVParser("/home/gammaliu/Datas/yago/yago3/yagoRelations/yago-relation-paraphrases.txt");
        for(Integer i = 1; i < pattys.size(); i++){
            RowYagoTriple triple = pattys.get(i);
            this.loderYagoRelation.saveRelatonLabelTripleTuple( triple );
        }
    }

    public void importAll(){
        importRelationGlosses();

        // TODO;
        //importRelationPatty();
    }


    /***********************************************************************
     *
     */
    public static void main(String[] args){
        TransformerRelation transformerRelation = new TransformerRelation();

        transformerRelation.importAll();
    }

}
